#!/usr/bin/python
#coding=gbk

'''
Created on 2011-702

@author: xiangbibo
'''

import unittest
from weibopy.auth import OAuthHandler
from weibopy.api import API

#class Test(unittest.TestCase):
class SinaCrawler:
    
    consumer_key= '201019872'
    consumer_secret ='2febe495b4012e31ce24a6f652e98893'
    
    def __init__(self):
            """ constructor """
    
    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def getAttValue(self, obj, key):
        try:
            return obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def auth(self):
        
        if len(self.consumer_key) == 0:
            print "Please set consumer_key"
            return
        
        if len(self.consumer_key) == 0:
            print "Please set consumer_secre"
            return
                
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        auth_url = self.auth.get_authorization_url()
        print 'Please authorize: ' + auth_url
        verifier = raw_input('PIN: ').strip()
        self.auth.get_access_token(verifier)
        self.api = API(self.auth)
  
    def setToken(self, token, tokenSecret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.setToken(token, tokenSecret)
        self.api = API(self.auth)
    
    def update(self, message):
        message = message.encode("utf-8")
        status = self.api.update_status(message)
        self.obj = status
        id = self.getAtt("id")
        text = self.getAtt("text")
        print "update---"+ str(id) +":"+ text
        
    def destroy_status(self, id):
        status = self.api.destroy_status(id)
        self.obj = status
        id = self.getAtt("id")
        text = self.getAtt("text")
        print "update---"+ str(id) +":"+ text

    def friends_of(self, id):
	res = {}
	i = 0
	while 1:
            timeline = self.api.friends(id=id, count=100, cursor=i)
	    i = i + len(timeline) 
            #print  i
	    if len(timeline) == 0: break
	    for line in timeline:
	        self.obj = line
                fid = self.getAtt("id")
                name = self.getAtt("screen_name")
	        res[fid] = name
                #print "friends---"+ str(fid) + ":"+ name
        return res

    def followers_of(self, id):
        res = {}
        i = 0
        while 1:
            timeline = self.api.followers(id=id, count=500, cursor=i)
            i = i + len(timeline) 
            #print  i
            if len(timeline) == 0: break
            for line in timeline:
                self.obj = line
                fid = self.getAtt("id")
                name = self.getAtt("screen_name")
		#if res.has_key(fid):
                #    print "friends---"+ str(fid) + ":"+ name + ":" + res[fid]
                res[fid] = name
                #print "friends---"+ str(fid) + ":"+ name.encode("utf8")
        return res

class UserLibrary:
    crawled_users   = {}
    new_users       = []
    crawled_log     = 'crawled.list'
    newuser_log     = 'new.list'

    def __init__(self):
        """ constructor """
        fcrawled = open(self.crawled_log, 'r')
        while 1:
            line = fcrawled.readline()
            if not line: break
            self.crawled_users[line.rstrip("\n")] = 1
        fcrawled.close()

        fnew = open(self.newuser_log, 'r')
        while 1:
            line = fnew.readline()
            if not line: break
            self.new_users.append(line.rstrip("\n"))
        fnew.close()
        
        self.fcrawled = open(self.crawled_log, 'a')

    def __del__(self):
        fnew = open(self.newuser_log, 'w')
        for user in self.new_users:
            fnew.write(user + "\n")
        fnew.close()

        self.fcrawled.close()

    def append(self, user_list):
        for user in user_list:
            if not self.crawled_users.has_key(user):
                self.crawled_users[user] = 1
                self.new_users.append(user)
                self.fcrawled.write(str(user) + "\n")

    def pop(self):
        userid = self.new_users.pop()
        return userid

test = SinaCrawler()
test.setToken("e7af080736dfec3040a7d6608fd3bc08", "ffd3850ad73042a70437c857d5a84143")
#test.update("test")
#test.destroy_status("13478610081")


ffriends = open('friends', 'a')
ffollowers = open('followers', 'a')

userlib = UserLibrary()
userlib.append(["1739416705"])

while 1:
    userid = userlib.pop()
    if userid == None: break
    print "crawl friends of ", userid
    friends = test.friends_of(userid)
    ffriends.write("friends of: " + str(userid) + "\n")
    for k,v in friends.items():
        ffriends.write(str(k) + "\n")
        #ffriends.write(str(k)+ ": " + v.encode("utf8") + "\n")

    print "crawl follows of ", userid
    followers = test.followers_of(userid)
    ffollowers.write("followers of: " + str(userid) + "\n")
    for k,v in followers.items():
        ffollowers.write(str(k)+ "\n")
        #ffollowers.write(str(k)+ ": "+ v.encode("utf8") + "\n")

    userlib.append(friends)
    userlib.append(followers)

ffriends.close()
ffollowers.close()
